#ifndef DOCENTITY_H
#define DOCENTITY_H

#include <QObject>
#include <QTreeWidgetItem>
#include <QAction>

class DocEntity : public QObject, public QTreeWidgetItem
{
    Q_OBJECT
public:
    enum ViewTypes {Short,Normal};
    enum RefreshType {Himself, Children};
    class Operations
    {
    private:
        struct Operation
        {
            bool enabled;
            QAction* action;
        };
        QMap <int, Operation> m_operations;
    public:
        enum OperationType{Enabled, Disabled, All};
        Operations();
        void turnOff();
        void addOperation(const int&Index, QAction* action);
        void setEnabled(const int&Index, const bool&enabled);
        QAction* operation(const int&Index);
        QList<QAction*> list(OperationType operationType=Enabled) const;
        bool isEnabled(const int&Index) const;
    };
    explicit DocEntity(QTreeWidgetItem* parent=0);
    int level() const;
    int id() const;
    int num() const;    
    Operations* operations();
    DocEntity* parent() const;
    QString status() const;
    virtual QWidget* view(ViewTypes viewType=Normal)=0;
    virtual void refresh(const RefreshType&refreshType)=0;
    virtual void giveBithTo()=0;
    virtual bool saveToDB()=0;
    void setState(const int& state);
    void setColor(const QColor&color);
    bool isChanged() const;
    virtual bool isCompleted();
    virtual bool isPerformed();
    virtual bool isDelinquent();
    virtual bool isEdited();
    int state() const;
protected:    
    virtual QString capTemplate();
    DocEntity(QTreeWidget* parent=0);
    virtual bool removeFromDB()=0;
    virtual bool isCorrect()=0;
    void setChanged(const bool&arg);
    bool checkCode();
private:
    int m_level;
    int m_id;
    int m_num;
    Operations m_operations;
    int m_state;
signals:
    void childWakedUp(DocEntity*);
    void removeItem(DocEntity*);
    void newItem(DocEntity*);
    void changed();
    void wasChanged(DocEntity*);
    void wasSaved(DocEntity*);
    void wasRefresh();
    void stateMessage(const QString&message, const int&timeout);
public slots:
    void setID(const int&ID);
    void setNum(const int&Num);
    void refresh();
    void remove();
    void save();
private slots:
    void onChanged();
};

#endif // DOCENTITY_H
